package edu.stanford.cs.cs294s;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;


public class BalloonOverlay extends BalloonItemizedOverlay<OverlayItem>{
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private ArrayList<Point> mPoints = new ArrayList<Point>();
	private Context mContext;
	private int tapIndex = -1;
	public boolean viewButton = false;
	private MainActivity parentActivity;
	
	public BalloonOverlay(Drawable defaultMarker, MapView mapView) {
		super(defaultMarker, mapView);
		// TODO Auto-generated constructor stub
	}
	public MainActivity getParentActivity() {
		return parentActivity;
	}

	public void setParentActivity(MainActivity parentActivity) {
		this.parentActivity = parentActivity;
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}
	
	@Override
	protected boolean onTap(int index) {
		OverlayItem item = mOverlays.get(index);
		Point point = mPoints.get(index);
		tapIndex = index;
		final int thisIndex = index;
		boolean isRecycled;
		if(viewButton){
			/*
			AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
			dialog.setTitle(item.getTitle());
			dialog.setMessage(item.getSnippet());
			dialog.show();
			*/
		}else{
			GeoPoint gpoint = new GeoPoint(point.getLatitude(), point.getLongitude());
			
			if (balloonView == null) {
				balloonView = new BalloonOverlayView(mapView.getContext(), viewOffset);
				clickRegion = (View) balloonView.findViewById(R.id.balloon_inner_layout);
				isRecycled = false;
			} else {
				isRecycled = true;
			}
		
			balloonView.setVisibility(View.GONE);
			
			List<Overlay> mapOverlays = mapView.getOverlays();
			if (mapOverlays.size() > 1) {
				hideOtherBalloons(mapOverlays);
			}
			
			balloonView.setData(createItem(index));
			
			MapView.LayoutParams params = new MapView.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, gpoint,
					MapView.LayoutParams.BOTTOM_CENTER);
			params.mode = MapView.LayoutParams.MODE_MAP;
			
			setBalloonTouchListener(thisIndex);
			
			balloonView.setVisibility(View.VISIBLE);
	
			if (isRecycled) {
				balloonView.setLayoutParams(params);
			} else {
				mapView.addView(balloonView, params);
			}
			
			mc.animateTo(gpoint);
		}
		return true;
	}
	
	void addOverlay(OverlayItem item, Point point){
		mOverlays.add(item);
		mPoints.add(point);
		populate();
	}
	
	@Override
	protected boolean onBalloonTap(int index){
		GeoPoint gpoint = createItem(index).getPoint();
		Point point = new Point(gpoint);
		parentActivity.showPointInfo(point);
		return true;
	}

}
